---@class Battle.SkillReadyEffComponent:Battle.EntityComponent
local SkillReadyEffComponent = Battle.CreateClass("SkillReadyEffComponent",Battle.EntityComponent)
Battle.SkillReadyEffComponent = SkillReadyEffComponent

local tempVec3 = Vector3.New()
function SkillReadyEffComponent:OnCreate()
    self.cdEffName = "CD_01"
    self.readyEffName = "CD_02"
    ---@type Battle.EffectBase
    self.cdEff = nil
    ---@type Battle.EffectBase
    self.readyEff = nil
    ---@type BottomCDEffect
    self.bottomCDEffect = nil

end
function SkillReadyEffComponent:OnDestroy()
    self:UnRegister()
    Battle.EffectShowMgr.DestroyEffect(self.cdEff)
    Battle.EffectShowMgr.DestroyEffect(self.readyEff)
    self.cdEff = nil
    self.readyEff = nil
end
function SkillReadyEffComponent:OnInitializedAfter()
    self.skill = self.actor.skillSystem:GetSkill1()
    if self.skill then
        self:Register()
        self:LoadEff()
    end
end
function SkillReadyEffComponent:Register()
    local eventSystem = self.actor.eventSystem
    eventSystem:Register(Battle.Event.ActorPosChanged,self.Event_PosChanged,self)
    eventSystem:Register(Battle.Event.SkillCDChange,self.Event_SkillCDChange,self)
end
function SkillReadyEffComponent:UnRegister()
    local eventSystem = self.actor.eventSystem
    eventSystem:UnRegister(Battle.Event.ActorPosChanged,self.Event_PosChanged,self)
    eventSystem:Register(Battle.Event.SkillCDChange,self.Event_SkillCDChange,self)

end
function SkillReadyEffComponent:LoadEff()
    tempVec3:Set(self.actor.pos.x,0.1,self.actor.pos.z)
    local f = Vector3.New(-1,0,0)
    self.cdEff = Battle.EffectShowMgr.PlayEffect("CD_01",tempVec3,f)
    self.readyEff = Battle.EffectShowMgr.PlayEffect("CD_02",tempVec3,f)
    self.bottomCDEffect = self.cdEff.obj:GetComponent(typeof(BottomCDEffect))
    self.bottomCDEffect:StartCD(self.skill.curCd,self.skill.maxCd)
    self.readyEff:SetActive(not self.skill:IsInCD())
end
function SkillReadyEffComponent:Event_PosChanged(args)
    tempVec3:Set(self.actor.pos.x,0.1,self.actor.pos.z)
    self.cdEff:SetPosV(tempVec3)
    self.readyEff:SetPosV(tempVec3)
end
---@param args BattleEvent
function SkillReadyEffComponent:Event_SkillCDChange(args)
    local skill = args.skill
    if skill ~= self.skill then
        return
    end
    if self.bottomCDEffect then
        self.bottomCDEffect:StartCD(args.skillCd,args.skillMaxCd)
    end
    self.readyEff:SetActive(not self.skill:IsInCD())
end